// fillings for sports header
#include "Includes.hpp" // includes for project
#include "Sports.hpp" // class declarations

class Sport;

Sports::Sports() {
    sportsLlist = new List(Sorted); // make new sports list
}

bool Sports::addSport() {
    Sport* tempsport = new Sport(robio.readName("sport")); // make new sport
    if(sportsLlist->add(tempsport)) { // add it to list
	return true; // if this goes well, return true
    }
    return false; // if not return false
}

bool Sports::addDivision() {
    char* tempname = robio.readName("sport"); // make and get a sport name
    Sport* tempsport = (Sport*)sportsLlist->remove(tempname); // make temporary
    // sport to work on
    if (tempsport) { // if 'tempsport' != NULL
	tempname = robio.readName("division"); // reuse tempname, get name of new division
	if (!((*tempsport).divisionLlist->in_list(tempname))) {
	    ifstream in(robio.readFileName("division", tempname)); // open file-stream with
	    // name read from user input
	    if (in.is_open()) { // check if 'in' is open
		Division* tempdivision = new Division(&in, tempname); // read new division from
		// file-stream 'in'
		if ((*tempsport).divisionLlist->add(tempdivision)) { // add division to list
		    return true; // return true
		}
	    } else { // if in is !open
		cout << "\n\tcould not open file"; // print error
	    } // if (in.is_open()) end
	} else { // if division is in list already
	    cout << "\n\t" << tempname << " is already in division list"; // print error
	} // if (!(tempsport.divisionLlist)) end
    } else { // if 'tempsport' == NULL
	cout << "\n\tcould not find " << tempname; // print error message
    } // if (tempsport) end
    return false; // return false if this point is reached
}

void Sports::printAllSports() {
    sportsLlist->display_list(); // print all members of players list to screen
}

void Sports::printSport(char* senttext) {
    sportsLlist->display_element(senttext); // print sport with 'senttext' to screen
}

Sports::~Sports() {
    for (int lvar = sportsLlist->no_of_elements(); lvar > 0; lvar--) { // go through list
	sportsLlist->destroy(lvar); // destroy one, then another one -
    } // for lvar end
    delete sportsLlist; // at last, delete the entire list
}


bool Sports::deleteSport(char* sportsname) {
    if(sportsLlist->in_list(sportsname)) {   //If sportsname is in the list, 
	if (sportsLlist->destroy(sportsname)) { // destroy the sport
	    return true; // return true if destruction went OK
	} else {
	    cout << "\n\tcould not destroy sport: " << sportsname; // print error
	}
    } else {
	cout << "\n\tCould not find sport: " << sportsname; //Print error
    }
    return false; // return false if this point is reached
}

void Sports::printWriteSchedule() {
    Sport* tempsport = (Sport*)sportsLlist->remove(robio.readName("sport")); // get sport
    Division* tempdiv = (Division*)tempsport->divisionLlist->remove(robio.readName("division")); // get divisjon
    char* tempfilename = robio.readFileName("schedule (this has no default)", NULL, 0); // read tempfile name
    if (strlen(tempfilename) > 0) {
	ofstream out(tempfilename); // open file-stream
	tempdiv->writeSchedule(&out); // send ofstream to function writing schedule
    } else {
	tempdiv->printSchedule(); // print schedule
    }
}

bool Sports::deleteDiv() {
    char* tempname = robio.readName("sport"); // make and get a sport name
    Sport* tempsport = (Sport*)sportsLlist->remove(tempname); // make temporary
    // sport to work on
    if (tempsport) { // if 'tempsport' != NULL
	tempname = robio.readName("division"); // reuse tempname, get name of new division
	if ((*tempsport).divisionLlist->in_list(tempname)) { // if division is in list
	    (*tempsport).divisionLlist->destroy(tempname); // destroy it
	    return true;
	}
	else {
	    cout << "\n\tThe division doesn't exist"; // print error
	}
    }
    return false;
}
